package com.example.module_main

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import androidx.appcompat.app.AlertDialog

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_base.path.RouterPath
import com.example.lib_base.view.BaseActivity
import com.example.lib_common.view.event.CustomEvent
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_main.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = RouterPath.PATH_MAIN_PAGE)
@AndroidEntryPoint
class MainActivity : BaseMvvmActivity<ActivityMainBinding>() {

    private val needUserPermission = arrayListOf(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.READ_PHONE_NUMBERS,
        Manifest.permission.ACCESS_FINE_LOCATION
    )
    //用户没有同意的权限
    private val unGrantPermission = arrayListOf<String>()
    override fun getLayoutId(): Int=R.layout.activity_main

    override fun initViews() {
        val hostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        val navHostController = hostFragment.navController
        mDataBinding.bnv.setupWithNavController(navHostController)

        //自动发起权限授权

        mDataBinding.main.postDelayed({
            autoRequestPermisstion()
        },3000)
    }

    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {

    }
    private fun autoRequestPermisstion(): Boolean {
        //判断系统版本之有在6.0 以后才需要动态授权
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            //清理一下啊未授权的用户权限
            unGrantPermission.clear()

            //发起用户权限授权
            needUserPermission.forEach {
                var permissionState = checkSelfPermission(it)
                if(permissionState == PackageManager.PERMISSION_DENIED){
                    unGrantPermission.add(it)
                }
            }

            if(unGrantPermission.size>0){
                requestPermissions(unGrantPermission.toTypedArray(),102)
                return false
            }
        }
        return true
    }

    /**
     *权限授权返回
     * */
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        //校验

        var hasUnGranted =false
        if(requestCode == 102 && Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            unGrantPermission.forEach {
                //判断用户取消的权限
                if(checkSelfPermission(it) == PackageManager.PERMISSION_DENIED){
                    hasUnGranted = true
                }
            }
        }

        //弹窗告诉用户去设置中心的应用详情里设置
        if(hasUnGranted){
            val  builder=
            AlertDialog.Builder(this@MainActivity)
            builder.setTitle("权限提示")
                .setMessage("没有权限,请在应用详情中设置")
                .setPositiveButton("去设置"){dialog,which->
                    dialog.dismiss()
                    hasUnGranted =false
                    openSettingAppDetail()
                }
                .setNegativeButton("取消"){dialog,_->
                    dialog.dismiss()
                    hasUnGranted =false
                }.show()

        }


    }

    private fun  openSettingAppDetail(){
        //隐式意图跳转
        var intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.fromParts("package",packageName,null)
        this@MainActivity.startActivity(intent)
    }


    /**
     *
     * */

    override fun handEvent(event: CustomEvent) {
        super.handEvent(event)
        when(event.type){
            100->{
          
            }
            //新的公告
            101->{

            }
        }
    }
}//新建一个项目配置